<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Loader **/
require_once 'Zead/Loader.php';

/** @see Zead_Manifest_Contents **/
require_once 'Zead/Manifest/Contents.php';

/** @see Zead_Manifest_Options **/
require_once 'Zead/Manifest/Options.php';

/** @see Zead_Manifest_Quizzes **/
require_once 'Zead/Manifest/Quizzes.php';

/**
 * Abstract class Zead_Manifest
 *
 * @category   Zead
 * @package    Zead_Manifest
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
abstract class Zead_Manifest
{
	/** @var Zead_Manifest_Contents **/
	protected $_contents;
	/** @var Zead_Manifest_Option **/	
	protected $_options;
	/** @var Zead_Manifest_Quiz **/	
	protected $_quizzes;

	/**
	 * Static factory method
	 *
	 * @param string $adapter The adapter name
	 * @param array  $args The applied the object adapter
	 * @return self
	 */
	public static function factory($adapter = null, $args)
	{
		if (null == $adapter) {
			$adapter = 'Null';
		}
		$className = 'Zead_Manifest_Adapter_'. ucfirst($adapter);
		
		try {
			Zead_Loader::loadClass($className);
			$class = new $className($args);		
			return $class;
		} catch (Exception $e) {
			throw $e;
		}
	}	
	
	/**
	 * Class constructor
	 * 
	 * @param array $args The arguments passed to adapter object
	 */
	protected function __construct($args)
	{
		// Initialize properties
		$this->setContents(new Zead_Manifest_Contents)
			->setOptions(new Zead_Manifest_Options)
			->setQuizzes(new Zead_Manifest_Quizzes());
		
		// Setup method. Implemented in adapter extended class
		$this->_setup($args);
	}
	
	/**
	 * Return the object contents node
	 * @return Zead_Manifest_Contents
	 */
	public function getContents()
	{
		return $this->_contents;
	}
	
	/**
	 * Sets the contents node object
	 * @param Zead_Manifest_Contents $contents
	 */
	public function setContents(Zead_Manifest_Contents $contents)
	{
		$this->_contents = $contents;
		return $this;
	}
	
	/**
	 * Creates a new content object
	 * @return Zead_Manifest_Content
	 */
	public function createContent()
	{
		return new Zead_Manifest_Content();
	}
	
	/**
	 * Appends a content to other content or the root contents
	 * @param Zead_Manifest_Content $content
	 * @param Zead_Manifest_Content
	 */
	public function appendContent(Zead_Manifest_Content $content, $parent = null)
	{
		if (null !== $parent && $parent instanceof Zead_Manifest_Content) {
			$parent->append($content);
		} else {
			$this->_contents->append($content);
		}
	}
	
	/**
	 * Return the object options node
	 * @return Zead_Manifest_Options
	 */
	public function getOptions()
	{
		return $this->_options;
	}
	
	/** Sets the options node object
	 * @param Zead_Manifest_Options $options
	 */
	public function setOptions(Zead_Manifest_Options $options)
	{
		$this->_options = $options;
		return $this;
	}
	
	/**
	 * Create a option node object
	 * @return Zead_Manifest_Option
	 */
	public function createOption()
	{
		return new Zead_Manifest_Option();
	}
	
	/**
	 * Append an node option to parent option object or to options list node.
	 * @param Zead_Manifest_Option $option
	 * @param Zead_Manifest_Option $parent
	 */
	public function appendOption(Zead_Manifest_Option $option, $parent = null)
	{
		if (null !== $parent && $parent instanceof Zead_Manifest_Option) {
			$parent->append($option);
		} else {
			$this->_options->append($option);
		}
	}	
	
	/**
	 * Return the quizzes node object
	 * @return Zead_Manifest_Quizzes
	 */
	public function getQuizzes()
	{
		return $this->_quizzes;
	}
	
	/**
	 * Sets the quizzes node object
	 * @param Zead_Manifest_Quizzes
	 */
	public function setQuizzes(Zead_Manifest_Quizzes $quizzes)
	{
		$this->_quizzes = $quizzes;
	}
	
	/***
	 * Creates a new quiz object node
	 * @return Zead_Manifest_Quiz
	 */
	public function createQuiz()
	{
		return new Zead_Manifest_Quiz();
	}	
	
	/**
	 * Appends a object quiz to quiz list
	 * @param Zead_Manifest_Quiz $quiz
	 */
	public function appendQuiz(Zead_Manifest_Quiz $quiz)
	{
		$this->_quizzes->append($quiz);
	}
	
	/**
	 * Creates a new quiz question object node
	 * @return Zead_Manifest_QuizQuestion
	 */	
	public function createQuizQuestion()
	{
		return new Zead_Manifest_Quiz_Question();
	}
		
	/**
	 * Abstract protected method _setup. Implemented in adapter
	 * @param array $args The arguments passed by factory class
	 */	
	abstract protected function _setup();
}
